#include "manager.h"
#include "ui_manager.h"
#include<QDebug>
#include<QMessageBox>
manager::manager(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::manager)
{
    ui->setupUi(this);
    //窗口初始化
    ui->join->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
}

manager::~manager()
{
    delete ui;
}

 void manager::setManagerController(ManagerController& manCon)
 {
     this->manCon=manCon;
     ui->id_label->setText(QString::fromStdString(this->manCon.getOrg().name)+"  "+
                 QString::fromStdString(this->manCon.getUser().name));
 }

void manager::inform(QString id){
//    this->id=id;
//    ui->id_label->setText(this->id);

}
//加盟审批
void manager::on_join_approve_clicked()
{
    ui->stackedWidget->setCurrentIndex(0);
    ui->add_window->setStyleSheet("background-color: #34495E");
    ui->join_approve->setStyleSheet("background-color: green");
    ui->window_approve->setStyleSheet("background-color: #34495E");
    ui->dish_approve->setStyleSheet("background-color: #34495E");

    manCon.selectOrgJoin(orgJoin.users,orgJoin.exam_ids);

    cur_organ_show();

}
void manager::cur_organ_show(){
//勾选框，商家id，商家名称,审批id
//    organ_name.clear();
//    busi_id.clear();
//    busi_name.clear();
//    for(auto user:orgJoin.users)
//    {
//        busi_id.append(QString::fromStdString(user.phone));
//        busi_name.append(QString::fromStdString(user.name));
//    }
//    for(auto eid:orgJoin.exam_ids)
//        organ_name.append(QString::number(eid));
//    organ_name.append("学一");busi_id.append("1");busi_name.append("兰州拉面");
//    organ_name.append("学二");busi_id.append("2");busi_name.append("新疆味道");
//    organ_name.append("学三");busi_id.append("3");busi_name.append("东北味道");
//    organ_name.append("学四");busi_id.append("4");busi_name.append("麻辣烫");
//    organ_name.append("学五");busi_id.append("5");busi_name.append("麻辣香锅");
//    qDebug()<<organ_name;
    int iRow = orgJoin.users.size();
    qDebug()<<QString("有")<<iRow<<QString("行");
    ui->join->setRowCount(iRow);
    for(int row=0;row<iRow;row++)
    {
    qDebug()<<QString("插入行：")<<row;
    QTableWidgetItem *check = new QTableWidgetItem();
    check->setCheckState(Qt::Unchecked);
    ui->join->setItem(row,0,check);
    ui->join->setItem(row,1,new QTableWidgetItem(QString::fromStdString(orgJoin.users[row].phone)));//商家id
    ui->join->setItem(row,2,new QTableWidgetItem(QString::fromStdString(orgJoin.users[row].name)));//商家名称
    ui->join->setItem(row,3,new QTableWidgetItem(QString("%1").arg(orgJoin.exam_ids[row])));//审批id，使用
    }
}
void manager::delete_organ(int i){

     busi_id.removeAt(i);
     busi_name.removeAt(i);
    organ_name.removeAt(i);
}



void manager::cur_organ_show1(){
//勾选框，商家id，商家名称，组织名称
    qDebug()<<organ_name;
    int iRow = organ_name.size()-1;
    qDebug()<<QString("有")<<iRow<<QString("行");
    ui->join->setRowCount(iRow);
    for(int row=0;row<=iRow;row++)
    {
    qDebug()<<QString("插入行：")<<row;
    QTableWidgetItem *check = new QTableWidgetItem();
    check->setCheckState(Qt::Unchecked);
    ui->join->setItem(row,0,check);
    ui->join->setItem(row,1,new QTableWidgetItem(busi_id[row]));
    ui->join->setItem(row,2,new QTableWidgetItem(busi_name[row]));
    ui->join->setItem(row,3,new QTableWidgetItem(organ_name[row]));
    }
}
//加盟申请通过
void manager::on_join_success_clicked()
{
    int status=ApproveExam::STATU_PASS;

    qDebug()<<QString("通过加盟申请");
    qDebug()<<QString("当前可选组织数量为:")<<organ_name.size();
    for(int i=0;i<organ_name.size()-1;i++){
       if(ui->join->item(i,0)->checkState()){
           qDebug()<< QString("当前选择了第")<<i<<QString("行");
           qDebug()<< QString("通过了")<<ui->join->item(i,2)->text();
           qDebug()<<QString("商家申请的")<<ui->join->item(i,3)->text()<<QString("审批");
           organ_suc_name.append(ui->join->item(i,3)->text());
           delete_organ(i);
           qDebug()<<QString("审核通过的信息已删除");
           //进行审批
           manCon.examJoin(orgJoin.exam_ids[i],status);
       }
    }
//    cur_organ_show1();
    on_join_approve_clicked();//重新查询
    qDebug()<<QString("组织为:")<<organ_suc_name;
     QMessageBox::information(this,tr("success"),tr("审核已通过"),QMessageBox::Yes);
}


//不通过审核
void manager::on_join_fail_clicked()
{
    int status=ApproveExam::STATUS_NOTPASS;
    qDebug()<<QString("通过加盟申请");
    qDebug()<<QString("当前可选组织数量为:")<<organ_name.size();
    for(int i=0;i<organ_name.size()-1;i++){
       if(ui->join->item(i,0)->checkState()){
           qDebug()<< QString("当前选择了第")<<i<<QString("行");
           qDebug()<< QString("拒绝了")<<ui->join->item(i,2)->text();
           qDebug()<<QString("商家申请的")<<ui->join->item(i,3)->text()<<QString("组织");
           organ_fail_name.append(ui->join->item(i,3)->text());
           delete_organ(i);
           qDebug()<<QString("审核不通过的信息已删除");
           //进行审批
           manCon.examJoin(orgJoin.exam_ids[i],status);
       }
    }
//    cur_organ_show1();
    on_join_approve_clicked();//重新查询
    qDebug()<<QString("组织为:")<<organ_fail_name;
     QMessageBox::information(this,tr("success"),tr("审核不通过"),QMessageBox::Yes);
}


























//窗口
void manager::on_window_approve_clicked()
{
ui->stackedWidget->setCurrentIndex(1);
    qDebug()<<QString("点击窗口审批");
    ui->add_window->setStyleSheet("background-color: #34495E");
    ui->join_approve->setStyleSheet("background-color: #34495E");
    ui->window_approve->setStyleSheet("background-color: green");
    ui->dish_approve->setStyleSheet("background-color: #34495E");
    //获取审批
    manCon.selectOrgExamWindow(windowExam.wins,windowExam.users,windowExam.exam_ids);
    qDebug()<<QString("获取到结果");
    cur_window_show();
    qDebug()<<QString("结果显示结束");
}


void manager::cur_window_show(){
//勾选框，窗口位置，窗口名称，用户姓名，用户电话，审批id

//    window_name.clear();
//    window_address.clear();
//    user_name.clear();
//    user_phone.clear();
//    exaim_id.clear();
//    for(auto win_n:windowExam.wins)
//    {
//        window_name.append(QString::fromStdString(win_n.name));
//        window_address.append(QString::fromStdString(win_n.address));
//    }
//    for(auto user:windowExam.users)
//    {
//        user_name
//    }


//    window_address.append("学一");window_type.append("日租");window_name.append("兰州拉面");
//    window_address.append("学二");window_type.append("利润");window_name.append("新疆味道");
//    window_address.append("学三");window_type.append("日租");window_name.append("东北味道");
//    window_address.append("学四");window_type.append("利润");window_name.append("麻辣烫");
//    window_address.append("学五");window_type.append("日租");window_name.append("麻辣香锅");
//    qDebug()<<window_name;
    qDebug()<<QString("*********显示窗口审批界面****************");
    int iRow = windowExam.users.size();
    qDebug()<<QString("有")<<iRow<<QString("行");

    ui->window->setRowCount(iRow);
    for(int row=0;row<iRow;row++)
    {
    qDebug()<<QString("插入行：")<<row;
    QTableWidgetItem *check = new QTableWidgetItem();
    check->setCheckState(Qt::Unchecked);
    ui->window->setItem(row,0,check);
    ui->window->setItem(row,1,new QTableWidgetItem(QString::fromStdString(windowExam.wins[row].address)));//窗口地址
    ui->window->setItem(row,2,new QTableWidgetItem(QString::fromStdString(windowExam.wins[row].name)));//窗口名称
    ui->window->setItem(row,3,new QTableWidgetItem(QString::fromStdString(windowExam.users[row].name)));//用户姓名
    ui->window->setItem(row,4,new QTableWidgetItem(QString::fromStdString(windowExam.users[row].phone)));//用户电话
    ui->window->setItem(row,5,new QTableWidgetItem(QString::number(windowExam.exam_ids[row])));//审批id
    }
}
void manager::cur_window_show1(){
//勾选框，商家位置，商家名称，收租方式
    qDebug()<<QString("再次显示信息");
    qDebug()<<window_name;
    int iRow = window_name.size()-1;
    qDebug()<<QString("有")<<iRow<<QString("行");
    ui->window->setRowCount(iRow);
    for(int row=0;row<=iRow;row++)
    {
    qDebug()<<QString("插入行：")<<row;
    QTableWidgetItem *check = new QTableWidgetItem();
    check->setCheckState(Qt::Unchecked);
    ui->window->setItem(row,0,check);
    ui->window->setItem(row,1,new QTableWidgetItem(window_address[row]));
    ui->window->setItem(row,2,new QTableWidgetItem(window_name[row]));
    ui->window->setItem(row,3,new QTableWidgetItem(window_type[row]));
    }
}


void manager::delete_window(int i){
     window_address.removeAt(i);
     window_type.removeAt(i);
    window_name.removeAt(i);
}
//窗口审批通过
void manager::on_pushButton_clicked()
{
    int status=ApproveExam::STATU_PASS;
    int exam=0;
    qDebug()<<QString("通过窗口申请**********************************");
    for(int i=0;i<=windowExam.users.size()-1;i++){
        qDebug()<<QString("第")<<i<<QString("行      ")<<ui->window->item(i,0)->checkState();
       if(ui->window->item(i,0)->checkState()){
           exam++;
           qDebug()<< QString("通过审批的窗口为")<<QString::fromStdString(windowExam.wins[i].address);
           qDebug()<< QString("当前选择了第")<<i<<QString("行");
           //进行审批
            manCon.examWindow(windowExam.exam_ids[i],windowExam.wins[i],status);

           qDebug()<< QString("通过了")<<ui->window->item(i,2)->text()<<QString("窗口");
           manCon.selectOrgExamWindow(windowExam.wins,windowExam.users,windowExam.exam_ids);
           cur_window_show();
           qDebug()<<QString("审核通过的信息已删除");
       }
    }
    if(exam==0){
        qDebug()<<QString("未选择审批通过的窗口，请重新选择");

    }
    else {
//    cur_window_show1();
    //on_window_approve_clicked();//重新查询
    manCon.selectOrgExamWindow(windowExam.wins,windowExam.users,windowExam.exam_ids);
    cur_window_show();
    QMessageBox::information(this,tr("success"),tr("审核已通过"),QMessageBox::Yes);
}
}

//窗口审批不通过
void manager::on_pushButton_2_clicked()
{
    int status=ApproveExam::STATUS_NOTPASS;

    qDebug()<<QString("通过窗口申请");
    for(int i=0;i<=windowExam.users.size()-1;i++){
       if(ui->window->item(i,0)->checkState()){
           qDebug()<< QString("当前选择了第")<<i<<QString("行");
           //进行审批
            manCon.examWindow(windowExam.exam_ids[i],windowExam.wins[i],status);
           qDebug()<< QString("通过了")<<ui->window->item(i,2)->text()<<QString("窗口");
//           window_fail_name.append(ui->window->item(i,3)->text());
           qDebug()<<QString("审核通过的信息已删除");
       }
    }
//    cur_window_show1();
     on_window_approve_clicked();//重新查询
    qDebug()<<QString("通过的窗口为:")<<window_fail_name;
     QMessageBox::information(this,tr("success"),tr("审核不通过"),QMessageBox::Yes);

}














//菜品审批
void manager::on_dish_approve_clicked()
{
    ui->stackedWidget->setCurrentIndex(2);
    qDebug()<<QString("显示需要审核的菜品信息");
    ui->add_window->setStyleSheet("background-color: #34495E");
    ui->join_approve->setStyleSheet("background-color: #34495E");
    ui->window_approve->setStyleSheet("background-color: #34495E");
    ui->dish_approve->setStyleSheet("background-color: green");

    //获取数据

    manCon.selectOrgExamFood(foodExam.sales,foodExam.wins);
    cur_dish_show();

}

void manager::cur_dish_show(){

    //设置图片信息
    dish_picture.append("D:/canteen-management-system1/banmian.jpg");dish_name.append("拌面");dish_type.append("组合");
    dish_price.append("15");dish_unit.append("份");dish_dirscription.append("fghjkjhgfgh");dish_window_name.append("版面");

    dish_picture.append("D:/canteen-management-system1/chaoshou.jpg");dish_name.append("抄手");dish_type.append("组合");
    dish_price.append("8");dish_unit.append("份");dish_dirscription.append("fghjkjhgfgh");dish_window_name.append("超市");

    dish_picture.append("D:/canteen-management-system1/keleji.jpg");dish_name.append("可乐鸡");dish_type.append("组合");
    dish_price.append("13");dish_unit.append("份");dish_dirscription.append("fghjkjhgfgh");dish_window_name.append("牛肉饭");

    dish_picture.append("D:/canteen-management-system1/shiguoji.jpg");dish_name.append("石锅鸡");dish_type.append("组合");
    dish_price.append("12");dish_unit.append("份");dish_dirscription.append("fghjkjhgfgh");dish_window_name.append("石锅饭");

    dish_picture.append("D:/canteen-management-system1/shiguojidan.jpg");dish_name.append("石锅鸡蛋");dish_type.append("组合");
    dish_price.append("10");dish_unit.append("份");dish_dirscription.append("fghjkjhgfgh");dish_window_name.append("石锅饭");

    dish_picture.append("D:/canteen-management-system1/shiguoyaxie.jpg");dish_name.append("石锅鸭血");dish_type.append("组合");
    dish_price.append("11");dish_unit.append("份");dish_dirscription.append("fghjkjhgfgh");dish_window_name.append("石锅饭");

    dish_picture.append("D:/canteen-management-system1/suanlafen.jpg");dish_name.append("酸辣粉");dish_type.append("酸辣粉");
    dish_price.append("6");dish_unit.append("份");dish_dirscription.append("fghjkjhgfgh");

    dish_picture.append("D:/canteen-management-system1/xunyafan.jpg");dish_name.append("熏鸭饭");dish_type.append("组合");
    dish_price.append("14");dish_unit.append("份");dish_dirscription.append("fghjkjhgfgh");dish_window_name.append("牛肉饭");


    ui->dish->setColumnWidth(1, 100);
    ui->dish->verticalHeader()->setDefaultSectionSize(100);

    int row_size=foodExam.sales.size();//获取审批数
    ui->dish->setRowCount(row_size);
    qDebug()<<QString("共有")<<row_size<<QString("条信息");
        for(int row=0;row<row_size;row++)
        {

            QTableWidgetItem *check = new QTableWidgetItem();
            check->setCheckState(Qt::Unchecked);


            QLabel *l1 = new QLabel();     //创建lable
            QPixmap pixmap(foodExam.sales[row]->image);//获取图片
            pixmap = pixmap.scaled(100, 100, Qt::KeepAspectRatio, Qt::SmoothTransformation);
            l1->setPixmap(pixmap);    //加载图片
            l1->setAlignment(Qt::AlignHCenter);      //设置居中


            ui->dish->setItem(row,0,check);
            ui->dish->setCellWidget(row,1,l1);     //显示
            ui->dish->setItem(row,2,new QTableWidgetItem(QString::fromStdString(foodExam.wins[row].name)));//窗口名称
            ui->dish->setItem(row,3,new QTableWidgetItem(QString::fromStdString(foodExam.sales[row]->name)));//菜品名称
            ui->dish->setItem(row,4,new QTableWidgetItem(QString::number(foodExam.sales[row]->price,'f',2)));//菜品价格
            ui->dish->setItem(row,5,new QTableWidgetItem(QString::fromStdString(foodExam.sales[row]->diningType)));//菜品类型
            ui->dish->setItem(row,6,new QTableWidgetItem(QString::fromStdString(foodExam.sales[row]->unit)));//菜品单位
            ui->dish->setItem(row,7,new QTableWidgetItem(QString::fromStdString(foodExam.sales[row]->desciption)));//菜品描述

   }
}

void manager::cur_dish_show1(){
    qDebug()<<QString("当前的还未通过的菜品为:")<<dish_window_name;
    ui->dish->setColumnWidth(1, 100);
    ui->dish->verticalHeader()->setDefaultSectionSize(100);
    ui->dish->setRowCount(dish_window_name.size());
    qDebug()<<QString("共有")<<dish_window_name.size()<<QString("条信息");
    ui->dish->setRowCount(dish_window_name.size());
        for(int row=0;row<=dish_window_name.size()-1;row++)
        {
            QTableWidgetItem *check = new QTableWidgetItem();
            check->setCheckState(Qt::Unchecked);
            QLabel *l1 = new QLabel();     //创建lable
            QPixmap pixmap(dish_picture[row]);
            pixmap = pixmap.scaled(100, 100, Qt::KeepAspectRatio, Qt::SmoothTransformation);
            l1->setPixmap(pixmap);    //加载图片
            l1->setAlignment(Qt::AlignHCenter);      //设置居中
            ui->dish->setItem(row,0,check);
            ui->dish->setCellWidget(row,1,l1);     //显示
            ui->dish->setItem(row,2,new QTableWidgetItem(dish_window_name[row]));
            ui->dish->setItem(row,3,new QTableWidgetItem(dish_name[row]));
            ui->dish->setItem(row,4,new QTableWidgetItem(dish_price[row]));
            ui->dish->setItem(row,5,new QTableWidgetItem(dish_type[row]));
            ui->dish->setItem(row,6,new QTableWidgetItem(dish_unit[row]));
            ui->dish->setItem(row,7,new QTableWidgetItem(dish_dirscription[row]));
   }
}

void manager::delete_dish(int i){
    dish_window_name.removeAt(i);
    dish_picture.removeAt(i);
    dish_name.removeAt(i);
    dish_type.removeAt(i);
    dish_price.removeAt(i);
    dish_unit.removeAt(i);
    dish_dirscription.removeAt(i);
}
void manager::print_dish(){
    qDebug()<<QString("***************");
    for(int i=0;i<dish_window_name.size();i++){
        qDebug()<<dish_window_name[i]<<dish_picture[i]<<dish_name[i]<<dish_type[i]<<dish_price[i]<<dish_unit[i]<<dish_dirscription[i];
    }
    qDebug()<<QString("***************");
}
//菜品审批通过

void manager::on_pushButton_3_clicked()
{
    int status=ApproveExam::STATU_PASS;

    qDebug()<<QString("通过菜品申请");
    qDebug()<<QString("当前菜品数量为:")<<dish_window_name.size();
    for(int i=0;i<dish_window_name.size();i++){
       if(ui->dish->item(i,0)->checkState()){
           //进行审批
           manCon.examFood(foodExam.sales[i],status);

           qDebug()<< QString("当前选择了第")<<i<<QString("行");
           qDebug()<< QString("通过了")<<ui->dish->item(i,3)->text()<<QString("菜");
           dish_suc_name.append(ui->dish->item(i,3)->text());
           delete_dish(i);
           qDebug()<<QString("菜单审核通过的信息已删除");
       }
    }
    qDebug()<<QString("当前的还未通过的菜品为:")<<dish_window_name;
//    cur_dish_show1();
    //重新查询
    on_dish_approve_clicked();

    qDebug()<<QString("通过的菜品为:")<<dish_suc_name;
     QMessageBox::information(this,tr("success"),tr("审核已通过"),QMessageBox::Yes);
}

//菜品审核不通过
void manager::on_pushButton_4_clicked()
{
    int status=ApproveExam::STATUS_NOTPASS;

    qDebug()<<QString("通过菜品申请");
    qDebug()<<QString("当前菜品数量为:")<<dish_window_name.size();
    for(int i=0;i<dish_window_name.size();i++){
       if(ui->dish->item(i,0)->checkState()){

           //进行审批
           manCon.examFood(foodExam.sales[i],status);

           qDebug()<< QString("当前选择了第")<<i<<QString("行");
           qDebug()<< QString("拒绝了")<<ui->dish->item(i,3)->text()<<QString("菜");
           dish_suc_name.append(ui->dish->item(i,3)->text());
           delete_dish(i);
           qDebug()<<QString("菜单审核通过的信息已删除");
       }
    }
    qDebug()<<QString("当前的还未通过的菜品为:")<<dish_window_name;
//    cur_dish_show1();
    //重新查询
    on_dish_approve_clicked();

    qDebug()<<QString("拒绝的菜品为:")<<dish_fail_name;
     QMessageBox::information(this,tr("success"),tr("审核已通过"),QMessageBox::Yes);
}













//添加窗口
void manager::on_add_window_clicked()
{
    ui->stackedWidget->setCurrentIndex(3);
    ui->add_window->setStyleSheet("background-color: green");
    ui->join_approve->setStyleSheet("background-color: #34495E");
    ui->window_approve->setStyleSheet("background-color: #34495E");
    ui->dish_approve->setStyleSheet("background-color: #34495E");

}


//确定添加窗口

void manager::on_pushButton_5_clicked()
{
    QString new_window_addr=ui->lineEdit->text();
    QString new_window_type=ui->comboBox->currentText();
    WindowInfo win;
    win.address=new_window_addr.toStdString();
    //todo:还需要添加窗口地址
    manCon.addWindow(win);
    QString message="窗口"+new_window_addr+"已添加，类型为"+new_window_type;
    QMessageBox::information(this,tr("success"),tr("窗口已添加"),QMessageBox::Yes);

}

